package projectx.web.base;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

import projectx.core.base.ProjectXSystemException;
import projectx.core.base.ProjectXUserException;
import projectx.transaction.DbTransaction;
import projectx.util.ClassUtil;

public abstract class ProjectXDbTrxController extends DbTransaction {

	// --------------------------------------------------------------------------
	// M E M B E R S
	//
	// --------------------------------------------------------------------------

	protected final Logger _log = Logger.getLogger(getClass().getName());
	protected final static String DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
	private I_ProjectXController _pc;
	
	public static void main(String[] args)
	{
		System.err.println(getCurrentTimeBackUp());	
		System.err.println(getCurrentTimeString());		
	}
	protected static String getCurrentTimeString() {
		SimpleDateFormat fmt = new SimpleDateFormat(DATE_PATTERN);
		return fmt.format(Calendar.getInstance().getTime());
	}
	public static java.sql.Date getCurrentTimeBackUp() {
		SimpleDateFormat formatter = new SimpleDateFormat(DATE_PATTERN);
		try {
			Date date = formatter.parse(getCurrentTimeString());
			return new java.sql.Date(date.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// --------------------------------------------------------------------------
	// C O N S T R U C T O R S
	//
	// --------------------------------------------------------------------------
	/**
	 * @param uc
	 *            uc
	 * @param form
	 *            form
	 */
	protected ProjectXDbTrxController(UserContext uc, ProjectXForm form) {
		super((String) uc.get(ConstantsProjectX.USER_ID));
		_pc = new ProjectXController(uc, form);
	}

	// --------------------------------------------------------------------------
	// M E T H O D S
	//
	// --------------------------------------------------------------------------

	public abstract Object execute() throws ProjectXUserException,
			ProjectXSystemException;

	public ProjectXForm getForm() {
		return _pc.getForm();
	}

	public UserContext getUc() {
		return _pc.getUc();
	}

	protected void debugBegin() {
		_log.debug(ClassUtil.getClassName(this) + " execute begin.");
	}

	protected void debugEnd() {
		_log.debug(ClassUtil.getClassName(this) + " execute end.");
	}
}
